<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<title>相亲信息管理</title>
<link rel="stylesheet" href="../lib/layui/css/layui.css">
</head>
<body class="layui-layout-body">
			<form class="layui-form">
				<!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
				<div class="layui-form-item">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-block">
						<input type="text" name="name" placeholder="请输入姓名"
							autocomplete="off" class="layui-input" id="name">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">姓别</label>
					<div class="layui-input-block">
						<input type="text" name="sex" placeholder="请输入性别"
							autocomplete="off" class="layui-input" id="sex">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">出生日期</label>
					<div class="layui-input-block">
						<input type="text" name="birthday" placeholder="请输入出生日期"
							autocomplete="off" class="layui-input" id="birthday">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">年龄</label>
					<div class="layui-input-block">
						<input type="text" name="age" placeholder="请输入年龄"
							autocomplete="off" class="layui-input" id="age">
					</div>
				</div>
				<div class="layui-form-item">
    				<label class="layui-form-label">城市</label>
   					 <div class="layui-input-block">
     					 <select name="city" lay-verify="required" id="type">
        				  	
     					 </select>
    				</div>
  				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">图片</label>
					<button type="button" class="layui-btn" id="test1">
						<i class="layui-icon">&#xe67c;</i>上传图片
					</button>
					<span id="imgname"></span><img src="" id="img" width="100px">
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit lay-filter="sub">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
				<!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
			</form>
	<script src="../lib/layui/layui.js"></script>
	<script src="../lib/js/jquery-1.8.2.js"></script>
							
	<script>
	
		//JavaScript代码区域	
		function getQueryString(name) {
								var reg = new RegExp("(^|&)" + name
										+ "=([^&]*)(&|$)", "i");
								var url = decodeURI(decodeURI(window.location.search))
								var r = url.substr(1).match(reg);
								if (r != null)
									return unescape(r[2]);
								return null;  
							}
							$(document).ready(function() {
								var id = getQueryString("id");
								var name = getQueryString("name");
								var birthday = getQueryString("birthday");
								var age = getQueryString("age");
								var city = getQueryString("city");
								var tupian = getQueryString("tupian");
								
								$("#name").val(name);
								$("#birthday").val(birthday);
								$("#age").val(age);
								$("#type").val(city);
								$("#img").attr("src", "/hhh/" + tupian);
								
							});  
		
		
		
		layui.use([ 'form', 'layer', 'upload' ], function() {
			var form = layui.form;
			var layer = layui.layer;
			var upload = layui.upload;
			var $ = layui.$;
			var uploadInst = upload.render({
				elem : '#test1' //绑定元素
				,
				method : "post",
				url : '../tupian'//上传接口
				,
				done : function(res) {
					//上传完毕回调
					console.log(res);
					if(res.code==0){
						$("#imgname").html(res.data);
						//$('#test1').val(res.data);	
					}
				},
				error : function() {
					//请求异常回调
					console.log(res);
				}
			});					
			
			form.on('submit(sub)', function(data) {
				console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
				data.field.img = $("#imgname").html();
				var id = getQueryString("id");
				data.field.id=id;
				$.ajax({
					url : "../editxinxi",
					type : "get",
					data : data.field,
					success : function(res) {
						layer.msg(res.data.name + res.msg);
					}
				});
				return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
			});
			
			$(function() { 
				$.ajax({
					url : "../city",
					type : "get",
					data : {},
					success : function(res) {
						var $select = $("#type");//下拉菜单
						for (var i = 0; i < res.data.length; i++) {
							$select
									.append("<option value='"+res.data[i].CityId+"'>"
											+ res.data[i].CityName
											+ "</option>");
						}
						form.render();

					},
					dataType : "json"

				});

				});

			
		});
	</script>
</body>
</html>